package ar.com.utn.openqsb.lip.ui;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFormattedTextField;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.Border;

import ar.com.utn.openqsb.lip.LIPModule;
import ar.com.utn.openqsb.lip.core.Exercise;
import ar.com.utn.openqsb.lip.core.LIPException;
import ar.com.utn.openqsb.lip.core.LIPManager;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

/**
 * New Problem Dialog, it allow us to create new exercise in the module.
 * 
 * @author rvega
 * 
 */
public class NewExerciseDialog extends JInternalFrame implements ActionListener {

	private static final long serialVersionUID = -413480305302237300L;

	private LIPModule mainFrame;

	private JTextField nameField = new JTextField(20);

	private JTextField varField = new JFormattedTextField(NumberFormat
			.getInstance());

	private JTextField conField = new JFormattedTextField(NumberFormat
			.getInstance());

	private JButton submit = new JButton("Ok");

	private JButton cancel = new JButton("Cancel");

	ButtonGroup functional = new ButtonGroup();

	public NewExerciseDialog(LIPModule mainFrame, JDesktopPane desktop) {
		this.mainFrame = mainFrame;
		// set default size an position
		setPreferredSize(new Dimension(330, 210));
		setResizable(false);
		setClosable(true);

		// design form
		FormLayout layout = new FormLayout(
				"center:75dlu, 10px, pref, 10px, pref ", // columns
				"10px, 20dlu, 20dlu, 20dlu, 10px, 20dlu"); // rows
		PanelBuilder builder = new PanelBuilder(layout);
		builder.setDefaultDialogBorder();
		CellConstraints cc = new CellConstraints();
		// name field
		builder.addLabel("Name", cc.xy(1, 2));
		builder.add(nameField, cc.xyw(3, 2, 3));
		// number of variables
		builder.addLabel("Variables", cc.xy(3, 3));
		varField.setText("0");
		builder.add(varField, cc.xy(5, 3));
		// number of constrains
		builder.addLabel("Constrains", cc.xy(3, 4));
		builder.add(conField, cc.xy(5, 4));
		conField.setText("0");

		// functional
		JPanel panel = new JPanel(new GridLayout(0, 1));
		panel.setPreferredSize(new Dimension(100, 50));
		Border border = BorderFactory.createTitledBorder("Functional");
		panel.setBorder(border);
		JRadioButton max = new JRadioButton("Maximize");
		max.setSelected(true);
		JRadioButton min = new JRadioButton("Minimize");
		panel.add(max);
		functional.add(max);
		panel.add(min);
		functional.add(min);
		builder.add(panel, cc.xywh(1, 3, 1, 2));

		// buttons
		builder.add(submit, cc.xy(3, 6));
		builder.add(cancel, cc.xy(5, 6));
		add(builder.getPanel());

		// configure button to handle events
		submit.addActionListener(this);
		cancel.addActionListener(this);

		// show dialog in the main panel
		desktop.add(this);
		pack();
		setVisible(true);

	}

	/**
	 * Handle form events.
	 */
	public void actionPerformed(ActionEvent event) {
		JButton button = (JButton) event.getSource();
		if (button.equals(submit)) {
			// Create new exercise model
			Exercise descriptor = new Exercise(nameField.getText(), Integer
					.valueOf(varField.getText()), Integer.valueOf(conField
					.getText()), functional.getElements().nextElement()
					.isSelected());
			try {
				LIPManager.getInstance().create(descriptor);
				this.mainFrame.newWindow(descriptor.getName());
				// Discard current dialog
				dispose();
				this.mainFrame.remove(this);
			} catch (LIPException e) {
				JOptionPane.showMessageDialog(this, e.getMessage(), "Error",
						JOptionPane.ERROR_MESSAGE);
			}

		} else if (button.equals(cancel)) {
			// Discard current dialog
			dispose();
			this.mainFrame.remove(this);
		}
	}

}
